package com.fancy.learncenter.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.fancy.learncenter.activity.base.BaseActivity;
import com.fancy.learncenter.bean.BaseDataBean;
import com.fancy.learncenter.bean.LoginDataBean;
import com.fancy.learncenter.bean.WxLoginDataBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.common.WeiXinUtils;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.net.IdeaObserver;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.SPUtils;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;
import com.fancy.learncenter.utils.WXShareUtil;
import com.journeyapps.barcodescanner.Util;
import com.superservice.lya.R;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import fancy.hyypaysdk.pay.PayCallBack;
import fancy.hyypaysdk.pay.wxpay.WXPayUtil;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.UserInfo;

public class LoginActivity extends BaseActivity {

    @Bind(R.id.ed_account)
    EditText edAccount;
    @Bind(R.id.ed_psw)
    EditText edPsw;
    @Bind(R.id.bt_login)
    Button btLogin;
    @Bind(R.id.tv_forget)
    TextView tvForget;
    @Bind(R.id.tv_regist)
    TextView tvRegist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login, getResources().getColor(R.color.toolbar_color));
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bt_login, R.id.tv_forget, R.id.wx_login, R.id.tv_regist})
    public void onViewClicked(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.bt_login:

                if (Utils.isChinaPhoneLegal(edAccount.getText().toString())) {
                    login();
                } else {
                    ToastUtil.show("请输入正确的账号");
                }
                break;
            case R.id.tv_forget:
                intent = new Intent(this, ForgetActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_regist:
                intent = new Intent(this, RegistActivity.class);
                startActivity(intent);
                break;
            case R.id.wx_login:

                WXPayUtil.getInstance().wxLogin(this, new PayCallBack() {
                    @Override
                    public void paySuccess() {
                        ToastUtil.show("登陆成功");
                        wxLogin();
                    }

                    @Override
                    public void payFaild() {
                        ToastUtil.show("登陆失败");
                    }

                    @Override
                    public void payCancle() {
                        ToastUtil.show("登陆取消");
                    }
                });
                break;
        }
    }

    private void login() {
        Map map = new HashMap();
        map.put("mobile", edAccount.getText().toString());
        map.put("password", edPsw.getText().toString());
        String sign = Utils.MD5("mobile=" + edAccount.getText().toString() + "&password=" + edPsw.getText().toString());
        map.put("sign", sign);
        HttpMehtod.getInstance().login(map, new IdeaObserver<BaseDataBean<LoginDataBean>>() {

            @Override
            public void onSuccess(BaseDataBean<LoginDataBean> loginDataBeanBaseDataBean) {

                MyApplication.token = loginDataBeanBaseDataBean.getData().getToken();
                MyApplication.userId = loginDataBeanBaseDataBean.getData().getUser_id();
                MyApplication.is_device = loginDataBeanBaseDataBean.getData().getIs_device() + "";


                SPUtils.putToken(loginDataBeanBaseDataBean.getData().getToken());
                SPUtils.putUserID(loginDataBeanBaseDataBean.getData().getUser_id());

                Utils.loginRongYun();
                Utils.savePosition();
                gotoMain();

            }

            @Override
            public void onFail(int errorCode) {
                LogUtil.MyLog("login", "===login==onFail==" + errorCode);
            }
        });
    }

    private void wxLogin() {
        Map map = new HashMap();
        map.put("code", MyApplication.getInstance().getWX_LOGIN_CODE());
        HttpMehtod.getInstance().wxLogin(map, new IdeaObserver<BaseDataBean<WxLoginDataBean>>() {

            @Override
            public void onSuccess(BaseDataBean<WxLoginDataBean> loginDataBeanBaseDataBean) {
                String code = loginDataBeanBaseDataBean.getData().getUnionId();

                if (!TextUtils.isEmpty(code)) {
                    Intent intent = new Intent(LoginActivity.this, BindWxActivity.class);
                    intent.putExtra("openId", "" + code);
                    startActivity(intent);
                    finish();
                } else {
                    MyApplication.token = loginDataBeanBaseDataBean.getData().getToken();
                    MyApplication.userId = loginDataBeanBaseDataBean.getData().getUser_id();
                    MyApplication.is_device = loginDataBeanBaseDataBean.getData().getIs_device() + "";


                    SPUtils.putToken(loginDataBeanBaseDataBean.getData().getToken());
                    SPUtils.putUserID(loginDataBeanBaseDataBean.getData().getUser_id());

                    Utils.loginRongYun();

                    gotoMain();
                }
            }

            @Override
            public void onFail(int errorCode) {
                LogUtil.MyLog("login", "===login==onFail==" + errorCode);
            }
        });
    }

    private void gotoMain() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    public static final int FRESH_RESULT_CALLBACK = 100100;

    @Override
    public void finish() {
        setResult(FRESH_RESULT_CALLBACK);
        super.finish();
    }
}
